package com.atguigu.gmall.serviceutil.minio;

import com.atguigu.gmall.serviceutil.properties.MinioProperties;
import com.atguigu.gmall.serviceutil.service.OssService;
import com.atguigu.gmall.serviceutil.service.impl.OssServiceImpl;
import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2025/10/22 3:36
 */
@Configuration
@EnableConfigurationProperties(MinioProperties.class)
public class AppMinioClientConfig {
    @Bean
    public MinioClient minioClient(MinioProperties minioProperties) throws Exception {
        //String endpoint 访问地址, String accessKey 访问密钥ID, String secretKey  访问密钥
        MinioClient minioClient = new MinioClient(minioProperties.getEndpoint(), minioProperties.getAccessKey(), minioProperties.getSecretKey());
        boolean exists = minioClient.bucketExists(minioProperties.getBucketName());
        if (!exists){
            minioClient.makeBucket(minioProperties.getBucketName());
        }
        return minioClient;
    }
    @Bean
    public OssService ossService(){
        return new OssServiceImpl();
    }
}
